home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / satan-1.1.1 / bin / get_targets < prev    next >
Text File  |  1996-04-24  |  2KB  |  64 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Scan a subnet for live hosts
  4. #
  5. # If given IP address or hostname, scan everything on that subnet.
  6. # If given partial IP address (e.g. 140.174.97), do the same.
  7. #
  8. # Method used:
  9. #
  10. #  Use fping to scan the net; any hits send to gethostbyaddr to
  11. # get the hostname.  This will print out a list of hostname and/or
  12. # IP addresses that are alive, one per line.
  13. #
  14.  
  15. require 'config/paths.pl';
  16. require 'perl/socket.pl';    # work around socket.ph problems
  17.  
  18. $name = $ARGV[0];
  19. if (! $name) { die "Usage: $0 network-address\n"; }
  20.  
  21. #
  22. # hostname?
  23. if ($name !~ /[0-9]+\.[0-9]+\.[0-9]+/) {
  24.     ($name, $aliases, $type, $len, @ip) = gethostbyname($name);
  25.     ($a,$b,$c,$d) = unpack('C4',$ip[0]);
  26.     $name = "$a.$b.$c";
  27.     }
  28.  
  29. #
  30. # IP addr?  If four octets, chop off the last one
  31. if ($name =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {
  32.     ($name) = ($name =~ /^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+/);
  33.     }
  34.  
  35. # 3 octets of an ip address:
  36. if ($name =~ /^[0-9]+\.[0-9]+\.[0-9]+$/) {
  37.     for $i (1..255) { $args .= "$name.$i "; }
  38.     }
  39. else { die "Can't figure out what to scan ($name)\n"; }
  40.  
  41.  
  42. # spawn off fping, look at results
  43. die "Can't execute $FPING" unless open(FPING, "$FPING $args |");
  44.  
  45. while (<FPING>) {
  46.     chop;
  47.     ($target, $result) = /(\S+)\s+(.*)$/;
  48.     if ($_ =~ /is unreachable/) { next; }
  49.     if ($_ =~ /is alive/) {
  50.         ($a,$b,$c,$d) = split(/\./, $target);
  51.         @ip = ($a,$b,$c,$d);
  52.         # Hack alert!! Some libcs dump when ahost has many addresses.
  53.         if (fork() == 0) {
  54.             ($name) = gethostbyaddr(pack("C4", @ip), &AF_INET);
  55.             if ($name) { print "$name\n"; }
  56.             else { print "$target\n"; }
  57.             exit;
  58.             }
  59.         else { wait; }
  60.         }
  61.     }
  62.  
  63. close(FPING);
  64.